from langchain_core.tools import Tool

from app.config.database_config import PgManager
from app.services.booking_service import BookingService
from app.services.flight_service import FlightService
from app.services.passenger_service import PassengerService

session = next(PgManager.get_db_static())
booking_service = BookingService(session)
flight_service = FlightService(session)
passenger_service = PassengerService(session)


def _get_available_seats_safe(flight_id):
    """安全获取可用座位，确保flight_id是整数"""
    return booking_service.get_available_seats(int(flight_id))


get_available_seats = Tool.from_function(
    func=_get_available_seats_safe,
    name="get_available_seats",
    description="根据航班号flight_id获取可用座位列表",
    parameters={
        "type": "object",
        "properties": {
            "flight_id": {
                "type": "integer",
                "description": "航班号ID"
            }
        },
        "required": ["flight_id"]
    }
)

from app.schemas.booking import BookingCreate

create_booking = Tool.from_function(
    func=booking_service.create_booking,
    name="create_booking",
    description="创建新的航班预订",
    args_schema=BookingCreate
)

get_booking_by_number = Tool.from_function(
    func=booking_service.get_booking_by_number,
    name="get_booking_by_number",
    description="根据预订号获取预订（包含航班和乘客详细信息）",
    parameters={
        "type": "object",
        "properties": {
            "booking_number": {
                "type": "string",
                "description": "预定号ID"
            }
        },
        "required": ["booking_number"]
    }
)

tools = [
    get_available_seats,
    create_booking,
    get_booking_by_number
]
